<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="lib/bootstrap.css">
    <script src="lib/jquery.js"></script>
</head>

<body style="padding: 20px;">
    <!-- 评论面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">发表评论</h3>
        </div>
        <form class="panel-body" id="form-comment">
            <div>品论人：</div>
            <input type="text" name="username" class="form-control">
            <div>品论内容：</div>
            <textarea name="content" class="form-control" rows="3"></textarea>
            <button type="submit" class="btn btn-sm btn-primary">发表评论</button>
        </form>
    </div>

    <!-- 评论列表 -->
    <ul class="list-group">
        <!-- <li class="list-group-item">
            <span class="badge" style="background-color:#f0ad4e ;">评论时间：</span>
            <span class="badge" style="background-color:#5bc0de ;">评论人：</span>
            Item 1
        </li> -->
    </ul>
    <script>
        $(function () {
            getCommentList();
            function getCommentList() {
                $.ajax({
                    method: "GET",
                    url: "http://www.liulongbin.top:3006/api/cmtlist",
                    success: res => {
                        if (res.status != 200) return alert("数据获取失败！");
                        var rows = [];
                        // res.data.map(v => {
                        //     rows.push(`<li class="list-group-item">
                        //     <span class="badge" style="background-color:#f0ad4e ;">评论时间：${v.time}</span>
                        //     <span class="badge" style="background-color:#5bc0de ;">评论人：${v.username}</span>
                        //     ${v.content}
                        //     </li>`)
                        // })
                        rows.push(res.data.map(v =>
                            `<li class="list-group-item">
                            <span class="badge" style="background-color:#f0ad4e ;">评论时间：${v.time}</span>
                            <span class="badge" style="background-color:#5bc0de ;">评论人：${v.username}</span>
                            ${v.content}
                            </li>`
                        ))
                        $(".list-group").empty().append(rows.join(""))
                    },
                    error: () => alert("获取数据失败")
                })
            }
            // 发表评论
            $("#form-comment").submit(function (e) {
                e.preventDefault();
                var data = $(this).serialize();
                $.ajax({
                    method: "POST",
                    url: "http://www.liulongbin.top:3006/api/addcmt",
                    data: data,
                    success: (res) => {
                        if (res.status != 201) return alert(res.msg);
                        getCommentList();
                        // 清空文本框,将jquery元素变成dom元素，这里的this是form表单
                        $(this)[0].reset();
                    },
                    error: res => alert("发表失败")
                })
            })
        })
    </script>
</body>

</html>